package com.ufgov.agcfs.baseData.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.SearchFilter;

import com.ufgov.agcfs.baseData.dao.EaccDao;
import com.ufgov.agcfs.baseData.entity.BookType;
import com.ufgov.agcfs.baseData.entity.Eacc;
import com.ufgov.agcfs.baseData.entity.UpdateType;
import com.ufgov.agcfs.pub.UUID;

@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class EaccService {

	@Autowired
	public EaccDao eaccDao;
	
	public List<Eacc> getEaccList(){
		return eaccDao.getEaccList();
	}
	
	public List<Eacc> findAll(){
//		return eaccDao.findAllEacc();
		Eacc eacc = new Eacc();		
		return eaccDao.findAllEacc(eacc);
	}

	public List<Eacc> findByNd(String nd){
		Eacc eacc = new Eacc();
		eacc.setNd(nd);
		return eaccDao.findAllEacc(eacc);
	}
	
	public List<Eacc> findSearch(
			Map<String, Object> searchParams, Sort sort) {
		
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		SearchFilter searchFilterBynd =filters.get("EQ_nd");
		String nd = "";
		if (searchFilterBynd!=null) {
			nd = (String) searchFilterBynd.value;
		}
		List<Eacc> eaccDetailList;
		if (nd == ""){
			Eacc eacc = new Eacc();
			eaccDetailList= eaccDao.findAllEacc(eacc);
		}
		else{
			eaccDetailList= eaccDao.findSearchEacc(nd);
		}
		return eaccDetailList;
	}

	public List<String> getAllNd(){
		Date nowTime=new Date(); 
		SimpleDateFormat dataFmt=new SimpleDateFormat("yyyy"); 
		String curYear = dataFmt.format(nowTime); 
		List<String> ndList = new ArrayList<String>();
		for (int i = -3; i <= 3; i++) {
			ndList.add(String.valueOf(Integer.parseInt(curYear)+i));
		}
		return ndList;
	}
	
	public void updateEacc(Eacc eacc){
		eaccDao.updateEacc(eacc);
	}
	
	public Eacc findByCode(String code){
		Eacc eacc = new Eacc();
		eacc.setEaccCode(code);
		List<Eacc> eaccList = eaccDao.findAllEacc(eacc);
		if (eaccList.size()==0){
			return null;
		} else {
			return (Eacc)eaccList.get(0);
		}
	}

	public void deleteEacc(Eacc eacc,Map<String, String> resultMap){
//		Eacc ea = findByCode(eacc.getEaccCode());
//		if (ea.equals(eacc)){
//			Eacc ea1 = new Eacc();
//			ea1.setNd(eacc.getNd());
//			ea1.setEaccCode(eacc.getEaccCode());
//			if (eaccDao.findAllEacc(ea1).size()>0){
//				getErrorMsg(resultMap,"saveError","标准经济科目已经使用，不可删除！");
//			}else
				eaccDao.delEacc(eacc);
//		} else {
//			getErrorMsg(resultMap,"saveError","标准经济科目与系统中不一致！");
//		}
		
	}
	
	public Eacc findByNdCode(String nd,String eaccCode){
		Eacc eacc = new Eacc();
		eacc.setNd(nd);
		eacc.setEaccCode(eaccCode);
//		List<Eacc> eaccList = eaccDao.findEacc(eacc);
		List<Eacc> eaccList = eaccDao.findAllEacc(eacc);
		if (eaccList.size()==0){
			return null;
		} else {
			return (Eacc)eaccList.get(0);
		}
	}
	
	public void saveEacc(Eacc eacc,Map<String, String> resultMap){
		if ((eacc.getId()==null)||(eacc.getId().equals(""))) {	
			Eacc eaccList = findByNdCode(eacc.getNd(),eacc.getEaccCode());
			if (eaccList==null) {
				eacc.setId(UUID.randomUUID().toString());
				eaccDao.insertEacc(eacc);
			} else {
				getErrorMsg(resultMap,"saveError",eacc.getNd()+"年代码为【"+eacc.getEaccCode()+"】的经济科目已经存在！");
				return;
			}
		} else {
			eaccDao.updateEacc(eacc);
		}
		
//		if (eaccList==null){
//			eaccDao.insertEacc(eacc);
//		} else {
//			eaccDao.updateEacc(eacc);
//		}	
	}
	
	private void getErrorMsg(Map<String, String> resultMap,String flg, String reason) {
		resultMap.put("result", "fail");
		resultMap.put(flg, reason);
		resultMap.put("reason", "保存失败！");
	}
}
